/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
//import javax.swing.text.*;

public class JColorButton extends JLabel implements MouseListener {
	private static final long serialVersionUID = 1L;

	public JColorButton(String name){
		super(name);
		addMouseListener(this);
		setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), BorderFactory.createEmptyBorder(1, 5, 1, 5)));
		setOpaque(true);
	}

	public void setColor(Color c){
		if (c==null){
			c=Color.BLACK;
		}
		setBackground(c);
		if (c.getRed()+c.getGreen()+c.getBlue()>384){
			setForeground(Color.BLACK);
		}else{
			setForeground(Color.WHITE);
		}
	}

	public Color getColor(){
		return getBackground();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		Color c=getColor();
		JColorPicker cp=new JColorPicker(c);
		cp.setLocationRelativeTo(this);
		cp.setVisible(true);
		c=cp.getColor();
		if (c!=null){
			setColor(c);
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}
}

